package com.xnyzc.lhy.mis.service.impl.cms.synchronous;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.*;
import com.xnyzc.lhy.mis.entity.cms.OaSysRole;
import com.xnyzc.lhy.mis.entity.system.OaSysUser;
import com.xnyzc.lhy.mis.entity.system.OaSysUserRole;
import com.xnyzc.lhy.mis.feign.heli.WorkSheetToHeLiService;
import com.xnyzc.lhy.mis.mapper.cms.OaSysRoleMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserRoleMapper;
import com.xnyzc.lhy.mis.service.cms.synchronous.SynchronousService;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.LoginSynchronousParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.RoleSynchronousParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  同步数据相关
 * </p>
 *
 * @author wangshuo
 * @since 2019-11-04
 */
@Service
public class SynchronousServiceImpl implements SynchronousService {

    @Autowired
    private OaSysRoleMapper oaSysRoleMapper;

    @Autowired
    private OaSysUserRoleMapper oaSysUserRoleMapper;

    @Autowired
    private OaSysUserMapper oaSysUserMapper;

    @Autowired
    private WorkSheetToHeLiService workSheetToHeLiService;

    /**
     * 登陆账号同步接口
     *
     * @return
     */
    @Override
    public Rv getLoginSynchronous(LoginSynchronousParam param) {
        // 参数检查
        if(CheckUtil.objIsEmpty(param.getLoginId())) {
            throw PangException.create(EErrorCode.missingArg);
        }

        //获取用户最新信息
        OaSysUser sysUser=oaSysUserMapper.selectById(param.getLoginId());

        // 用户不存在
        if(CheckUtil.objIsEmpty(sysUser)){
            throw PangException.create(EErrorCode.noData);
        }

        // 设置用户信息
        param.setUserToken(sysUser.getSysUserToken());
        param.setLoginName(sysUser.getLoginName());
        param.setStatus("1".equals(sysUser.getLoginFlag()) ? "ENABLE" : "DISABLE");

        return workSheetToHeLiService.getLoginSynchronous(param);
    }

    /**
     * 客服角色同步接口
     *
     * @return
     */
    @Override
    public Rv getRoleSynchronous(RoleSynchronousParam param) {
        //参数检查
        if(CheckUtil.objIsEmpty(param.getRoleId())){
            throw PangException.create(EErrorCode.missingArg);
        }

        // 获取角色信息
        OaSysRole role=oaSysRoleMapper.selectById(param.getRoleId());

        if(CheckUtil.objIsEmpty(role)){
            throw PangException.create(EErrorCode.unData);
        }

        param.setRoleId(String.valueOf(role.getRoleId()));
        param.setRoleName(role.getName());

        //获取角色下用户id列表
        QueryWrapper<OaSysUserRole> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq(OaSysUserRole.ROLE_ID,param.getRoleId());
        List<OaSysUserRole> userRoleList = oaSysUserRoleMapper.selectList(roleQueryWrapper);
        List<String> userList=new ArrayList<>();
        if(!userRoleList.isEmpty()){
            for(OaSysUserRole userRole : userRoleList){
                userList.add(String.valueOf(userRole.getUserId()));
            }
        }

        if(userList.size() > 0){
            param.setLoginId(StringUtils.join(userList.toArray(), "&"));
        }

        return workSheetToHeLiService.getRoleSynchronous(param);
    }
}
